package com.lz.yz.outing.controller;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.outing.util.UploadOSSFile;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@RequestMapping("upload")
@Controller
public class UploadController {

    private Logger log = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    UploadOSSFile uploadOSSFile;

    /**
     * 图片上传
     *
     * @param img
     * @return
     */
    @ApiOperation(value = "图片上传", response = ReturnValue.class)
    @RequestMapping(value = "images", method = RequestMethod.POST)
    @ResponseBody
    public ReturnValue saveImg(@RequestParam("img") MultipartFile[] img) {
        try {
            StringBuilder consb = new StringBuilder();
            for (MultipartFile file : img) {
                String remarkDescUrl = uploadOSSFile.uploadFile(file.getInputStream(),
                        file.getOriginalFilename());
                consb.append(remarkDescUrl + ",");
            }
            if (consb != null && consb.length() > 0) {
                return ReturnValue.generateTrueReturnValue(consb.toString().substring(0, consb.lastIndexOf(",")));
            }
        } catch (Exception e) {
            log.error("图片上传异常,异常信息：{}", e);
        }
        return ReturnValue.generateFalseReturnValue("图片上传异常");
    }

}
